package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.Result;
import com.itheima.pojo.Blog;
import com.itheima.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description:
 * 文章详情相关接口
 *
 * @author chenzhi
 * @date 2020/8/7
 */
@RestController
@RequestMapping("/content")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class WebBlogContentController {

    @Autowired
    private BlogService blogService;

    /**
     * 通过uid获取博客内容
     *
     * @param uid
     * @return
     */
    @GetMapping("/getBlogByUid")
    public Result getBlogByUid(@RequestParam("uid") String uid) {
        String data = null;
        try {
            data = blogService.getBlogByUid(uid);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "操作失败");
        }
        return new Result("success", data);
    }

    /**
     * 通过uid获取博客收藏数
     *
     * @param uid
     * @return
     */
    @GetMapping("/getBlogPraiseCountByUid")
    public Result getBlogPraiseCountByUid(@RequestParam("uid") String uid) {
        Integer data = null;
        try {
            data = blogService.getBlogPraiseCountByUid(uid);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "操作失败");
        }
        return new Result("success", data);
    }


    //根据BlogUid获取同类型的博客
    @GetMapping("/getSameBlogByBlogUid")
    public Result getSameBlogByBlogUid(@RequestParam("uid") String uid) {
        List<String> blogContentList = null;
        try {
            String blogSortUid = blogService.getBlogSortUid(uid);
            blogContentList = blogService.getBlogContentList(blogSortUid);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "操作失败");
        }
        return new Result("success", blogContentList);
    }

    //根据标签获取相关的博客
    @GetMapping("/getSameBlogByTagUid")
    public Result getSameBlogByTagUid(@RequestParam("tagUid") String tagUid,@RequestParam(name = "currentPage",required = false) Integer currentPage,@RequestParam(name = "pageSize",required = false) Integer pageSize) {

        com.itheima.entity.Page result = new com.itheima.entity.Page();
        try {
            Page<Blog> blogContentList =  blogService.getBlogContentListPage(tagUid,currentPage,pageSize);
            result.setTotal((int) blogContentList.getTotal());
            result.setRecords(blogContentList.getResult());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "查询数据异常");
        }

        return new Result("success", result);
    }

    //根据uid点赞博客
    @GetMapping("/praiseBlogByUid")
    public Result praiseBlogByUid(@RequestParam("uid") String uid) {

        try {
            blogService.updateCollectCount(uid);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error","点赞失败" );
        }

        return new Result("success","点赞成功" );
    }
}
